package com.santu.edu.supporter.biz.order.kd.dao;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.santu.edu.supporter.biz.buildings.pojo.dto.BuildingsDTO;
import com.santu.edu.supporter.biz.order.common.pojo.enums.OrderStatusEnum;
import com.santu.edu.supporter.biz.order.kd.dao.mapper.KuaidiOrderMapper;
import com.santu.edu.supporter.biz.order.kd.pojo.dto.KdOrderDTO;
import com.santu.edu.supporter.biz.order.kd.pojo.params.KdOrderUpdateParam;
import com.santu.edu.supporter.biz.order.kd.pojo.po.KuaidiOrderPO;
import com.santu.edu.supporter.util.PageData;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author luoyaoqi
 * @createTime 2024/2/20
 * @description
 */
@Repository
@RequiredArgsConstructor
public class KuaidiOrderDao {

    private final KuaidiOrderMapper kdOrderMapper;
    public void addOrder(KdOrderDTO dto) {
        KuaidiOrderPO item = KuaidiOrderPO.createItem(dto);
        kdOrderMapper.insertOne(item);
    }

    public void updateOrder(KdOrderDTO dto) {
        kdOrderMapper.updateOrderAddress(dto.getOrderId(),dto.getAddressId());
    }

    public KdOrderDTO getOrderById(String orderId) {
        KuaidiOrderPO item = kdOrderMapper.selectById(orderId);
        if (item == null) {
            return null;
        }
        return KdOrderDTO.createItem(item);
    }

    public PageData<KdOrderDTO> getOrderPage(String schoolId, String userId, Integer orderType, int page, int size) {
       Page<KuaidiOrderPO> pageData =  kdOrderMapper.selectOrderPage(schoolId, userId, orderType,new Page<>(page,size));
        List<KuaidiOrderPO> recordList = pageData.getRecords();
        if (CollUtil.isEmpty(recordList)) {
            return new PageData<>(Collections.emptyList(),pageData.getPages(),pageData.getTotal());
        }
        List<KdOrderDTO> dtoList = recordList.stream().map(KdOrderDTO::createItem).collect(Collectors.toList());
        return new PageData<>(dtoList,pageData.getPages(),pageData.getTotal());
    }

    public void updateOrderStatus(String orderId, OrderStatusEnum orderStatus,OrderStatusEnum newOrderStatus) {
        kdOrderMapper.updateOrderStatus(orderId,orderStatus.getCode(),newOrderStatus.getCode());
    }

    public void updateGoodComplete(String orderId) {
        kdOrderMapper.updateGoodComplete(orderId);
    }
}
